package com.jz.netthings.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.bean.system.JyCode;
import com.jz.netthings.bean.system.JyCodeType;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.support.SimpleCoreResponse;
import com.jz.netthings.system.mapper.mysql.JyCodeMapper;
import com.jz.netthings.system.service.JyCodeService;
import com.jz.netthings.system.service.JyCodeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yyy
 * @ClassName AdscmSystemController
 * @Description 用户管理Controller
 * @date 2018/8/1 17:34
 */
@RestController
@RequestMapping("/adscm/system/codeType")
public class AdscmCodeTypeController {
      @Autowired
      JyCodeTypeService jyCodeTypeService;
      @Autowired
      JyCodeService jyCodeService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JyCodeType jyCodeType) throws AdscmSystemException {
        boolean insert = jyCodeTypeService.insert(jyCodeType);
        return new SimpleCoreResponse("200", "新增成功", insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JyCodeType jyCodeType) throws AdscmSystemException {
        boolean result = jyCodeTypeService.updateById(jyCodeType);
        return new SimpleCoreResponse("200", "修改成功", result);
    }

    @RequestMapping("select")
    public SimpleCoreResponse select() throws AdscmSystemException {
        List<JyCodeType> jyCodeTypes = jyCodeTypeService.selectList(null);
        return new SimpleCoreResponse("200", "查询成功", jyCodeTypes);
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id) throws AdscmSystemException {

        Wrapper<JyCode> jyCodeWrapper =new EntityWrapper<>();
        jyCodeWrapper.eq("type",id);
        List<JyCode> jyCodes = jyCodeService.selectList(jyCodeWrapper);
        if(jyCodes.size()>0){
            throw  new AdscmSystemException( "不能删除该码表，请先删除码表下的子码表");
        }
        boolean b = jyCodeTypeService.deleteById(id);
        return new SimpleCoreResponse("200", "删除成功", b);
    }

}
